<?php
$this->beginBlock('toolbar');
$t = $this->toolbar();
echo $t->renderBegin();
echo $t->renderItem($this->link($this->__('system.button.back'), $this->historyUrl('*/*/*/index')));
echo $t->renderEnd();

$this->nextBlock('form');
$f = $this->modelForm;
$f->createField($this->__('cms.category_select'), TRUE)
  ->setInput($this->cmsCategorySelect('cms_category_id', 'model[argument][cms_category_id]'));
$f->createField($this->__('cms.content_select'), TRUE)
  ->setInput($this->select('cms_content_id', 'model[argument][cms_content_id]')
                  ->setOption($this->__('system.label.default_option'), ''));
echo $f->renderBegin();
echo $f->renderChildren();
echo $this->hidden('url', 'model[url]');
echo $f->renderEnd();

$t2 = $this->toolbar();
echo $t2->renderBegin();
echo $t2->renderItem($this->linkButton($this->__('system.button.save'), "submitForm('form')"));
echo $t2->renderEnd();
?>
<script language="javascript">
var contentSelected = '<?php echo $this->model->getCmsContentId() ?>';
function cmsCategoryChange(contentId){
    $('#cms_content_id').empty()
                        .append('<option value=""><?php echo $this->__('system.label.default_option') ?></option>');
    var cid = $('#cms_category_id').val();
    if(cid == ''){
        return;
    }
    var url = '<?php echo $this->buildUrl('*/cms/category/contents')?>'
    jQuery.get(url + 'id/' + cid, function(data){
       data = eval('(' + data + ')');
       for(var i = 0; i < data.contents.length; i++){
           $('#cms_content_id').append('<option value="' + data.contents[i].id + '">' + data.contents[i].title + '</option>');
       }
       if(contentId){
           $('#cms_content_id option[value="' + contentId + '"]').attr('selected', 'selected');
       }       
    });
}

cmsCategoryChange(contentSelected);
$('#cms_category_id').change(cmsCategoryChange);
$('#cms_content_id').change(function(){
    $('#url').val('/frontend/cms/content/index/id/' + $(this).val());
});
</script>
<?php
$this->endBlock();
echo $this->extend('template'.DS.'form');